package com.topxgun.gcode.controller;

import com.topxgun.gcode.utils.ConfigUtils;
import com.topxgun.gcode.utils.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/")
@Controller
public class IndexController {

    public static final String adminUsername = ConfigUtils.getPropertiesByKey("admin.username");
    public static final String adminPassword = ConfigUtils.getPropertiesByKey("admin.password");

    @RequestMapping("/")
    public String index(){
        return  "index";
    }

    @RequestMapping("/admin/login")
    public ModelAndView login(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/page/code/generation_code");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(StringUtils.isEmpty(username)){
            mv.addObject("code","100");
            mv.addObject("message","用户名不能为空");
            mv.setViewName("/admin/index");
            return mv;
        }else if(StringUtils.isEmpty(password)){
            mv.addObject("code","100");
            mv.addObject("message","用户名不能为空");
            mv.setViewName("/admin/index");
            return mv;
        }else if(!adminUsername.equals(username)){
            mv.addObject("code","100");
            mv.addObject("message","用户名不正确");
            mv.setViewName("/admin/index");
            return mv;
        }else if(!adminPassword.equals(password)){
            mv.addObject("code","100");
            mv.addObject("message","密码 不正确");
            mv.setViewName("/admin/index");
            return mv;
        }
        return  mv;
    }
}
